package com.bestcem.xm.user.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.deliver.grpc.v1.services.BpDeliverServiceGrpc;
import com.bestcem.xm.deliver.grpc.v1.services.CheckOrgHasUnfinishDeliverRequest;
import com.bestcem.xm.deliver.grpc.v1.services.CheckOrgHasUnfinishDeliverResponse;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * 投放相关服务
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/14 20:28
 */
@Slf4j
@Service("userBpDeliverGrpcService")
public class UserBpDeliverGrpcService {

    @GrpcClient(GrpcClientNames.DS_DELIVER)
    private BpDeliverServiceGrpc.BpDeliverServiceBlockingStub stub;

    /**
     * 根据租户id校验是否有未完成的投放信息
     *
     * @param orgId 租户id
     * @return java.lang.Boolean
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/06/14 20:30
     */
    public Boolean checkOrgHasUnfinishDeliver(String orgId) {
        if (log.isInfoEnabled()) {
            log.info("根据租户id校验是否有未完成的投放信息：orgId:{}", orgId);
        }

        CheckOrgHasUnfinishDeliverRequest.Builder builder = CheckOrgHasUnfinishDeliverRequest.newBuilder();
        builder.setOrgId(orgId);
        CheckOrgHasUnfinishDeliverRequest request = builder.build();
        // 请求投放服务
        CheckOrgHasUnfinishDeliverResponse response = stub.checkOrgHasUnfinishDeliver(request);
        // 校验查询结果
        if (Code.ERROR_VALUE == response.getStatus().getCodeValue()) {
            log.error("根据租户id校验是否有未完成的投放信息失败：dto:{} orgId:{}", response.getStatus().getMessage(), orgId);
            return Boolean.FALSE;
        }

        return response.getPassed();
    }

}
